import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    name: "韩梅梅",
    age: 16
  },
  getters: {
    dbAge(state) {
      return state.age * 2 
    }
  },
  mutations: {
    changeName(state,name) {
        state.name = name
    }
  },
  actions: {
    // 第一个参数是上下文 包含了commit 等一些方法
    // changeNameAction(,payload) {
    //   console.log(context)
    //   console.log("触发action")
    // }
    // changeNameAction(ctx,payload) {
    //   console.log(ctx)
    //   ctx.commit("changeName",payload)
    //   console.log("触发action")
    // }
    changeNameAction({ commit },payload) {
      commit("changeName",payload)
      console.log("触发action")
    }
  },
})
